$(document).ready(function() {
    initSize()
    var sessionId = localStorage.getItem('sessionId');
    if (sessionId != null && sessionId != "") {
        window.location.href = "/icr/html/index.html";
    }

})
//登录验证
$(".btn-info").click(function() {
    if ($("#username").val() == '') {
        $('#login-msg').find('label').eq(0).html('请输入用户名');
    } else if ($("#password").val() == '') {
        $('#login-msg').find('label').eq(0).html('请输入密码');
    } else {
        login();
    }

});
//回车确认事件
document.onkeydown = function(event) {
    if (event.keyCode == 13) {
        $("#login-submit").trigger("click");
    }
}


/*初始化默认字体大小*/
function initSize() {
    let fontSize;
    resize();
    window.onresize = resize;

    function resize() {

        let winW = window.innerWidth;
        if (winW >= 1440) { fontSize = winW / 1440 * 100 }
        document.getElementsByTagName('html')[0].style.cssText = "font-size:" + fontSize + "px";
    }

}

function login() {
    $('.submit').find('span').html('');
    var accout = $("#username").val();
    var password = $("#password").val();
    var url = '/rest/icr/login';
    $.ajax({
        url: url,
        type: "POST",
        contentType: "application/json",
        data: JSON.stringify({
            "username": accout,
            "password": password
        }),
        success: function(data) {
            if (data.status == 200 && data.result.sessionId) {
                let log = {};
                log.account = data.result.account;

                localStorage.setItem('sessionId', data.result.sessionId);
                localStorage.setItem('username', data.result.name);
                localStorage.setItem('loginUser', JSON.stringify(log));
                window.location.href = "index.html";
            } else {
                $('#login-msg').find('label').eq(0).html('用户名或密码错误');
            }
        },
        error: function(result) {

        }
    });
}